<?php

namespace app\model;

use GuzzleHttp\Client;
use think\Model;
use think\model\relation\HasOne;

class SupplierStation extends Model
{
    public function searchStation()
    {
        $param = request()->param();
        //获取渠道已上架的油站
        $ids = $this->where('supplier_id', $param['id'])->column('station_id');
        return app(StationBasic::class)->when(!empty($ids), function ($query) use ($ids) {
            $query->whereNotIn('id', $ids);
        })->where('state', '=', 1)->field("id `key`,name `label`")->select();
    }

    public function addStation(): array
    {
        $param = request()->post();
        $station_id = $param['station_id'];
        unset($param['station_id']);
        $items = array_map(function ($item) use ($param) {
            return array_merge(['station_id' => $item], $param);
        }, $station_id);
        $items = array_filter($items);
        $this->saveAll(array_values($items));
        $this->pushStation($param['supplier_id'], $station_id);
        return $items;
    }

    public function station(): HasOne
    {
        return $this->hasOne(StationBasic::class, 'id', 'station_id');
    }

    public function lst(): array
    {
        $param = request()->param();
        $data = $this->with(['station' => function ($query) {
            $query->field("id,name,default_price,number,rate_unit")->with(['oil' => function ($query) {
                $query->field("station_id,default_price,oil,rate_unit");
            }]);
        }])->when(!empty($param['name']), function ($query) use ($param) {
            $id = StationBasic::whereLike('name|number', "%{$param['name']}%")->column("id");
            $query->whereIn('station_id', $id);
        })->where('supplier_id', $param['supplier'])->order('create_time', 'desc')->paginate([
            'list_rows' => $param['size'] ?? 30,
            'page' => $param['page'] ?? 1,
        ])->toArray();
        if (!empty($data['data'])) {
            foreach ($data['data'] as &$value) {
                $oil = array_column($value['station']['oil'], null, 'oil');
                unset($value['station']['oil']);
                $value['station']['92_default_price'] = $oil['92']['default_price'] ?? 0;
                $value['station']['95_default_price'] = $oil['95']['default_price'] ?? 0;
                $value['station']['98_default_price'] = $oil['98']['default_price'] ?? 0;
                $value['station']['0_default_price'] = $oil['0']['default_price'] ?? 0;
                $value['station']['0_unit'] = $oil['0']['rate_unit'] ?? 1;
                $value['station']['92_unit'] = $oil['92']['rate_unit'] ?? 1;
                $value['station']['95_unit'] = $oil['95']['rate_unit'] ?? 1;
                $value['station']['98_unit'] = $oil['98']['rate_unit'] ?? 1;
            }
        }

        return pageData($data);
    }

    public function price()
    {
        $param = request()->param();
        $supplier = $param['supplier'];
        $this->saveAll($param['station']);
        $this->pushStation($supplier, array_column($param['station'], 'station_id'), "update_price");
        return $param;
    }

    public function deleteStation(): bool
    {
        $param = request()->param();
        $supplier = $param['supplier'];
        $id = $param['id'];
        $station_id = $param['station_id'];
        $this->whereIn('id', $id)->delete();
        $this->pushStation($supplier, $station_id, "delete");
        return true;
    }

    protected function pushStation($supplier, $station_id, $type = "add"): void
    {
        $supplier = app(Supplier::class)->find($supplier);
        $station = app(StationBasic::class)->whereIn('id', $station_id)->column("number");
        if (!empty($supplier['station_url']) && !empty($station)) {
            //推送油站更新信息
            foreach ($station as $value) {
                push_notice($supplier['station_url'], ['station_id' => $value, "type" => $type]);
            }
        }
    }
}
